package com.aiguigu;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//资源类
public class Ticket {
    private int num = 50;

//    public synchronized void sale(){
//        if(num>0){
//            System.out.println(Thread.currentThread().getName()+"\t"+"出售"+(num--)+"\t"+"还剩下："+num);
//        }
//    }

    private final Lock lock = new ReentrantLock(true);// ReentrantLock 叫可重入锁+默认悲观锁+ 默认非公平锁 true开启公平锁

    public void sale() {
        lock.lock();
        try {
            if (num > 0) {
                System.out.println(Thread.currentThread().getName() + "\t" + "出售" + (num--) + "\t" + "还剩下：" + num);
            }

        } finally {
            lock.unlock();
        }
    }

}
